<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");

$errMsg = "";
if(sxREQ("reqtype") == "save"){
	$errMsg .= libraryMod();
}
if(sxREQ("reqtype") == "delete"){
	$errMsg .= libraryDel();
}
$sSQL = "SELECT * FROM library WHERE library='".sxREQ('lbr')."'";
$objSDAO->query($sSQL);
$row=$objSDAO->fetch();
sxRDef('new_library', $row['library']);
sxRDef('description', $row['description']);
sxRDef('note', $row['note']);

$aOpz = array("kMenu"=>'pro.fun', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
function confDel(){
	if (confirm('Comfirm?')) {
		document.objFrm.reqtype.value = 'delete';
		document.objFrm.submit();
	}
}
</script>
  
<?php
echo(bodyHTML($aOpz));

$aCmd = array("delLib"=>array("pag"=>"javascript:confDel()",
										"title"=>"delete",
										"text"=>"delete"
										),
					"insLib"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>"save",
										"text"=>"save"
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('lbr');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Library</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".getHtmlText('new_library',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',80,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo $sCmd;
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function libraryMod(){
	global $objSDAO;
	$errMsg = "";
	if(trim(sxREQ('new_library')) == '')
		$errMsg .= "missing name<br>";
	if(ereg("[^a-zA-Z0-9_]",trim(sxREQ('new_library'))))
		$errMsg .= "Name: only letter, number and _<br>";
	if(trim(sxREQ('description')) == '')
		$errMsg .= "missing description<br>";
	//existence
	if (sxREQ('new_library')!=sxREQ('lbr')) {
		$sSQL = "SELECT count(*) as num FROM library WHERE library='".sxREQ('new_library')."'";
		$objSDAO->query($sSQL);
		$row = $objSDAO->fetch();
		if ($row['num']>0) {
			$errMsg .= "existing name<br>";
		}		
	}	
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "UPDATE library SET library = '".sxRAS("new_library")."' "
			.",description = '".sxRAS("description")."' " .
			", note = '".sxRAS("note")."' " .
			" WHERE library = '".sxRAS('lbr')."' ";			
	$objSDAO->query($sSQL,"ins");
	if (sxREQ('new_library')!=sxREQ('lbr')) {
		$sSQL = "UPDATE function SET library='".sxREQ('new_library')."' "
			." WHERE library='".sxREQ('lbr')."' ";
		$objSDAO->exec($sSQL);
	}	
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}

function libraryDel(){
	global $objSDAO;
	$errMsg = "";
	$sSQL = "SELECT function FROM function WHERE library='".sxREQ('lbr')."'";
	$objSDAO->query($sSQL);
	if ($row=$objSDAO->fetch()) {
		$errMsg .= "existing functions<br>";
	}	
	if($errMsg != ""){
		return $errMsg;
	}
	$sSQL = "DELETE FROM library " .
			" WHERE library = '".sxRAS('lbr')."' ";			
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}
